package com.zzj.service;

import com.zzj.base.Result;
import com.zzj.utils.JWTUtil;
import com.zzj.utils.RedisUtil;
import com.zzj.utils.Utils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Service
public class RedisService {
    @Resource
    private RedisUtil redisUtil;

    public final static String VerificationCode = "Cloud-Disk-VerificationCode";
    public final static long VerificationCodeExpirationTime = 5 * 60;  //5分钟
    public final static String VerificationNum = "Cloud-Disk-VerificationNum";
    public final static long VerificationCodeDayNum = 5; //一天获取验证码次数
    private final static String TOKEN = "Cloud-Disk-Token";
    private final static String REFRESH_TOKEN = "Cloud-Disk-RefreshToken";
    private final static String USERNAME = "Cloud-Disk-Username";

    private final static String ANNOUNCEMENT="Cloud-Disk-Announcement";
    private final static String SignedInDay="Cloud-Disk-SignedInDay";


    /**
     * 登录 token RefreshToken存储
     *
     * @param token
     * @param refreshToken
     * @param username
     */
    public void setRefreshToken(String token, String refreshToken, String username) {
        redisUtil.hset(refreshToken, TOKEN, token);
        redisUtil.hset(refreshToken, USERNAME, username);
        redisUtil.expire(refreshToken, JWTUtil.REFRESH_TOKEN_EXPIRE_TIME);
    }

    /**
     * token 是否在黑名单
     * @param token
     * @return
     */
    public boolean isBlacklist(String token) {
        return redisUtil.hget(JWTUtil.BLACK_KEY, token) != null;
    }



    /**
     * 加入黑名单
     */
    public void addBlackList(String token) {
        redisUtil.hset(JWTUtil.BLACK_KEY, token, 1, JWTUtil.TOKEN_EXPIRE_TIME / 1000);
    }
    /**
     * 验证 验证码
     * 成功后删除
     */
    public Result<?> verifyVerificationCode(String email, String code) {
        Object verificationCode = redisUtil.hget(VerificationCode, email);
        if (verificationCode == null) {
            return Result.error("验证码未获取或已过期");
        }
        if (!Objects.equals(code, String.valueOf(verificationCode))) {
            return Result.error("验证码错误");
        }
        redisUtil.hdel(VerificationCode, email);
        return Result.success();
    }

    /**
     * 创建 验证码 5分钟后过期
     */
    public Result<?> createVerificationCode(String email, String code) {
        Object verificationNum = redisUtil.hget(VerificationNum, email);
        if (verificationNum != null && Integer.parseInt(String.valueOf(verificationNum)) >= VerificationCodeDayNum) {
            return Result.error("验证发送次数今日已达上限");
        }
        redisUtil.hset(VerificationNum, email, verificationNum == null ? 1 : Integer.parseInt(String.valueOf(verificationNum)) + 1, Utils.getSecondsNextEarlyMorning());
        redisUtil.hset(VerificationCode, email, code, VerificationCodeExpirationTime);
        return Result.success();
    }

    public String getAnnouncement(){
        Object o = redisUtil.get(ANNOUNCEMENT);
        if (o == null) {
            return "";
        }
        return String.valueOf(o);
    }
    public Result setAnnouncement(String text){
        boolean set = redisUtil.set(ANNOUNCEMENT, text);
        if(!set){
            return  Result.error("设置失败");
        }
        return Result.success("设置成功");
    }

    public String getSignedInDay(String username){
        Object o = redisUtil.hget(SignedInDay,username);
        if (o == null) {
            return "";
        }
        return String.valueOf(o);
    }

    public Result signeIn(String username){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");
        String dateString = sdf.format(new Date());
        String signedInDay = getSignedInDay(username);
        boolean hset = redisUtil.hset(SignedInDay, username, signedInDay + "," + dateString);
        if(!hset){
            return  Result.error("签到失败");
        }
        return Result.success("签到成功");

    }
}
